<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.brooklynmarathon.checkout.Util" %>
<%@ page import="com.brooklynmarathon.checkout.Customer" %>
<%@ page import="com.brooklynmarathon.checkout.EnqueuePayQueue" %>
<%@ page import="com.brooklynmarathon.checkout.PMF" %>

<html>
  <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>

  <body>

<%
try
{
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user != null) {
%>
<p>Hello, <%= user.getNickname() %>! (You can
<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in</a>
to include your name with EnqueuePayQueues you post.</p>
<%
    }
%>

<%
    PersistenceManager pm = PMF.get().getPersistenceManager();

    String where = "";
    String query = null;
    if (request.getParameter("callerReference") != null)
    {
      where = " where callerReference == callerReferenceParameter parameters String callerReferenceParameter ";
      query = "select from " + EnqueuePayQueue.class.getName() + where + " order by created desc, date_sent desc range 0,10";
    }
    else
    {
      query = "select from " + EnqueuePayQueue.class.getName() + " order by created desc, date_sent desc range 0,10";
    }

    List<EnqueuePayQueue> EnqueuePayQueues = null;
    if (request.getParameter("callerReference") != null)
      EnqueuePayQueues = (List<EnqueuePayQueue>) pm.newQuery(query).execute(request.getParameter("callerReference"));
    else
      EnqueuePayQueues = (List<EnqueuePayQueue>) pm.newQuery(query).execute();

    if (EnqueuePayQueues.isEmpty()) {
%>
<p>no messages.</p>
<%
    } else {

    %>
    <table class="sample">
	<tr>
	    <th>Sent</th>
	    <th>To Send</th>
	    <th>Created</th>
	    <th>Response</th>
	    <th>Request</th>
	</tr>
    <%

        for (EnqueuePayQueue g : EnqueuePayQueues) {
%>
<tr class="d0">
    <td> <%= g.getDate_sent() %>  </td>
    <td> <%= g.getDate_to_send() %> </td>
    <td> <%= g.getCreated() %> </td>

<td>
    <% if(g.getResponse() != null){ %>
    <%= Util.escape(g.getResponse().getValue()) %>  
    <% } %>
</td>

<td>
    <% if(g.getUrl() != null){ %>
    <%= Util.escape(g.getUrl()).replaceFirst(".*callerReference=","") %> 
    <% } %>
</td>

    </tr>



<%
        }
	%>
    </table>

	<%




        for (EnqueuePayQueue g : EnqueuePayQueues) {
            if (g.getId() == null) {
%>
<p>An anonymous person wrote:</p>
<%
            } else {
%>
<p><b><%= g.getId() %></b> wrote:</p>
<%
            }
%>
<blockquote>
    sent: <%= g.getDate_sent() %>  <br/>
    to_send: <%= g.getDate_to_send() %> <br/>
    created: <%= g.getCreated() %> <br/>
    <% if(g.getResponse() != null){ %>
    response: <%= Util.escape(g.getResponse().getValue()) %>  <br/>
    <% } %>
    url: <%= g.getUrl() %>  <br/>
    <%=g.getCreated()%></blockquote>



<%
        }
    }
    pm.close();

    }
    catch (Exception e)
    {
    %>
    Error: <%=e.getMessage()%>
    <%
    }
%>


  </body>
</html>
